// pages/ucenter/order-list/order-list.js
var app = getApp();
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    allOrder_flag:0,
    waitPayOrder_flag:0,
    receivedOrder_flag:0,
    lostOrder_flag:0,
    //discount_flag:0,
    swipertab: [{ name: '全部', index: 0 }, { name: '待付款', index: 1 }, { name: '待收货', index: 2 },{ name: '退款/售后', index: 3 }],
    allOrder:[
    { flag:1, id: "123456789",
      order:[{name:"追源杨梅干1",status: "100克/袋;酸甜原味",num:"1",money:"30",url: "/images/1.png"},
      {name:"追源杨梅干2",status: "200克/袋;酸甜原味",num:"2",money:"40",url: "/images/1.png"}], 
      state: "交易成功", time: "2021-02-24 01:03:03",
      total_money: "132" ,total_num:"3",
      total_discount:"",comment_flag:"0"}, 
       { flag:2, id: "123456789",
      order:[{name:"追源杨梅干1",status: "100克/袋;酸甜原味",num:"1",money:"30",url: "/images/1.png"},
      {name:"追源杨梅干2",status: "200克/袋;酸甜原味",num:"2",money:"40",url: "/images/1.png"}], 
      state: "待付款", time: "2021-02-24 01:03:03",
      total_money: "132" ,total_num:"3",
      total_discount:"10",comment_flag:"1"}, 
      { flag:3, id: "123456789",
      order:[{name:"追源杨梅干1",status: "100克/袋;酸甜原味",num:"1",money:"30",url: "/images/1.png"},
      ], 
      state: "待发货", time: "2021-02-24 01:03:03",
      total_money: "132" ,total_num:"1",
      total_discount:"",comment_flag:"1"}, 
     
    /* {flag:1, id: "123456789",name:"追源杨梅干", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:3, id: "123456789",name:"追源杨梅干",  state: "待发货", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:4, id: "123456789",name:"追源杨梅干",  state: "待收货", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:5, id: "123456789",name:"追源杨梅干",  state: "退款中", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:6, id: "123456789",name:"追源杨梅干",  state: "已退款", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:7, id: "123456789",name:"追源杨梅干",  state: "已取消", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:1, id: "123456789",name:"追源杨梅干",  state: "交易成功", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:1, id: "123456789",name:"追源杨梅干",  state: "交易成功", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:1, id: "123456789",name:"追源杨梅干",  state: "交易成功", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" },
    {flag:1, id: "123456789",name:"追源杨梅干",  state: "交易成功", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" } */
  
  ],
    waitPayOrder: [],
    receivedOrder:[],
    lostOrder:[],
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo){
      wx.showModal({
          title:'提示',
          content:'您还没有登录',
          success(res){
              if (res.confirm){
                  // wx.switchTab({
                  //   url: '/pages/ucenter/index/index',
                  // })
                  app.getuserInfo();
              } else if (res.cancel){
                  wx.switchTab({
                      url: '/pages/index/index',
                  })
              }
          }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.orderClassification()
    this.orderShow()
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo == null){
      app.getuserInfo();
    }
  },
  onShareAppMessage:function(){
    return {
      title:'sharetest',
      path:'/pages/index/index?share=' + '54185418'
    }
  },


/**
 * @Explain：订单状态分类转义函数
 *
 * @author 赵泽雨
 * @date 2021/02/25 00：05
 */
  orderClassification:function(){
    var that = this
    var allOrder = that.data.allOrder
    var waitPayOrder = that.data.waitPayOrder
    var receivedOrder = that.data.receivedOrder
    var lostOrder = that.data.lostOrder
    for(var i = 0;i <allOrder.length; i++){
        if(allOrder[i].flag == 2){ 
          waitPayOrder.push(allOrder[i])
          waitPayOrder.forEach((r) => {  //array是后台返回的数据
            r.state = "待付款";   //r = array[0]的所有数据，这样直接 r.新属性 = 属性值 即可
          })
          that.setData({ //这里划重点 需要重新setData 下才能js 和 wxml 同步，wxml才能渲染新数据
            waitPayShow: that.data.waitPayShow
          })              
      }else if(allOrder[i].flag == 3 || allOrder[i].flag == 4){
          receivedOrder.push(allOrder[i])
      }else if(allOrder[i].flag == 5||allOrder[i].flag == 6){
          lostOrder.push(allOrder[i])
      }
    }
    this.setData({
      waitPayOrder:that.data.waitPayOrder,
      receivedOrder:that.data.receivedOrder,
      lostOrder:that.data.lostOrder
    })
    console.log(waitPayOrder)
  },

/**
 * @Explain：获取用户手机高度，以确定swiper长度
 *
 * @author 赵泽雨
 * @date 2021/02/25 00：05
 */
  getDeviceInfo: function () {
    var that = this 
    wx.getSystemInfo({ 
        success: function (res) { 
            that.setData({ 
                clientHeight: res.windowHeight 
            }); 
        } 
    }) 
  },
 
  /**
 * @Explain：选项卡点击切换
 *
 * @author 赵泽雨
 * @date 2021/02/25 00：05
 */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },
 
  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
    this.orderShow()
  },

 /*  discountShow:function(){
    var that = this
    var allOrder = that.data.allOrder
    for(var i = 0;i <allOrder.length; i++){
      console.log(this.data.allOrder[i].total_discount)
      console.log(this.data.discount_flag)
    if(this.data.allOrder[i].total_discount!=0){
      this.setData({
        discount_flag:1
      })         
    }
  }
  }, */
 
/**
 * @Explain：订单展示函数,若没有对应4栏的订单，则显示no-data页面
 *
 * @author 赵泽雨
 * @date 2021/02/25 00：05
 */
  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        if(this.data.allOrder.length!=0){
          this.setData({
            allOrder_flag:1
          })         
        }
        that.allShow()
        break
      case 1:
        if(this.data.waitPayOrder.length!=0){
          this.setData({
            waitPayOrder_flag:1
          })         
        }
        that.waitPayShow()
        break
      case 2:
        if(this.data.receivedOrder.length!=0){
          this.setData({
            receivedOrder_flag:1
          })         
        }
        that.receivedShow()
        break
      case 3:
        if(this.data.lostOrder.length!=0){
          this.setData({
            lostOrder_flag:1
          })         
        }
        that.lostShow()
        break
    }
  },
  allShow: function(){
      /* this.setData({
    allOrder: [{ id: "123456789",name:"追源杨梅干", state: "交易成功", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "132" }, { id: "123456789",name:"追源杨梅干",  state: "交易成功", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "205" }]
  }) */
  },
 
  waitPayShow:function(){
/*     this.setData({
      waitPayOrder: [{id: "123456789",name:"追源杨梅干",  state: "待付款", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味始", url: "/images/1.png", money: "186" }],
    }) */
  },
 
  receivedShow: function () {
/*     this.setData({
      receivedOrder: [{ id: "123456789",name:"追源杨梅干",  state: "待收货", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "122" }],
    }) */
  },

  lostShow: function () {
    /* this.setData({
      lostOrder: [{id: "123456789",name:"追源杨梅干",  state: "已退款", time: "2021-02-24 01:03", status: "100克/袋;酸甜原味", url: "/images/1.png", money: "122" }],
      
    
    
    }) */
  },
  /**
   * bindonclick 点击跳转事件
   * @author 徐志鹏
   * @date 2021/02/25
   * @param {*} e 
   */
  bindonclick:function(e){
    let index = e.currentTarget.dataset.index;
    let allorder = this.data.allOrder;
    let orderlist = [];
    let order = {};

    for (let i of allorder[index].order){
      order.name = i.name;
      order.picUrl = i.url;
      order.id = i.id;
      orderlist.push(order);
    }

    wx.navigateTo({
      url: '/pages/ucenter/add_comment/add_comment?orderlist=' + JSON.stringify(orderlist),
    })
  },
/**
   * orderDetailsClick 点击跳转事件
   * @author 赵泽雨
   * @date 2021/02/25
   * @param {*} e 
   */

  orderDetailsClick:function(e){
    /* let index = e.currentTarget.dataset.index;
    let allorder = this.data.allOrder;
    let orderlist = [];
    let order = {};

    for (let i of allorder[index].order){
      order.name = i.name;
      order.picUrl = i.url;
      order.id = i.id;
      orderlist.push(order);
    }

    wx.navigateTo({
      url: '/pages/ucenter/order_details/order_details?orderId=' + JSON.stringify(orderlist),
    }) */
    wx.navigateTo({
      url: '/pages/ucenter/order_details/order_details?orderId=',
    }) 
  }


  

})
